package onlinejudge.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import onlinejudge.dao.UserDao;
import onlinejudge.entity.User;
import onlinejudge.service.UserService;
import onlinejudge.vo.UserVo;

@Service
public class UserServiceImpl implements UserService {

	@Resource
	private UserDao userDao;
	private Map<String, Object> parameters=new HashMap<String, Object>();
	
	@Override
	public UserVo login(User user) {
		parameters.clear();
		parameters.put("userName", user.getUserName());
		parameters.put("passWord", user.getPassWord());
		List<UserVo> userList=userDao.queryUser(parameters);
		//用户名或密码错误
		if(userList.size()==0) {
			return null;
		}
		return userList.get(0);
	}

	@Override
	public User register(User user) {
		parameters.clear();
		parameters.put("userName",user.getUserName());
		List<UserVo> userList=userDao.queryUser(parameters);
		//该身份证已注册
		if(userList.size()==1) {
			return null;
		}else if(userList.size()==0) {	//该身份证没注册过
			parameters.put("userName", user.getUserName());
			parameters.put("passWord", user.getPassWord());
			parameters.put("email", user.getEmail());
			parameters.put("userImg", user.getUserImg());
			parameters.put("roleId", 1);
			userDao.addUser(parameters);
		}
		return user;
	}

	@Override
	public void updateUser(UserVo user) {
		parameters.clear();
		parameters.put("userId", user.getUserId());
		parameters.put("userName", user.getUserName());
		parameters.put("passWord", user.getPassWord());
		parameters.put("email", user.getEmail());
		parameters.put("userImg", user.getUserImg());
		userDao.updateUser(parameters);
	}

	@Override
	public void deleteUser(Integer userId) {
		userDao.deleteUser(userId);
	}

	@Override
	public List<UserVo> queryAllUser(Map<String, Object> parameters1) {
		parameters1.put("roleName", "client");
		return userDao.queryUser(parameters1);
	}

	@Override
	public int getUserCount() {
		parameters.clear();
		parameters.put("roleName", "client");
		return userDao.queryUser(parameters).size();
	}

	@Override
	public UserVo getUser(Integer userId) {
		parameters.clear();
		parameters.put("userId", userId);
		return userDao.queryUser(parameters).get(0);
	}

}
